//
//  MoreViewController.m
//  ios_news
//
//  Created by Han on 14-6-9.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "MoreViewController.h"
#import "GridCell.h"
#import "Grid.h"
#import "Util.h"
#import "FavoriteViewController.h"
#import "Gallery.h"
#import "MoreGalleryView.h"
#import "DeclarationViewController.h"
#import "FeedbackViewController.h"
#import "ActViewController.h"
#import "RecommendViewController.h"

@implementation MoreViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.navigationController.navigationBar.barTintColor = MAIN_COLOR;
    self.navigationController.navigationBar.translucent = NO;  // 半透明
    self.navigationController.navigationBar.hidden = NO;
    
    self.navigationItem.title = @"更多";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    mDataList = [Grid getData]; 
    //NSLog(@"%@", mDataList);
    
    [self addGallery];
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    mGridView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 120, 320, GLOBLE_HEIGHT -  120) collectionViewLayout:flowLayout];
    mGridView.backgroundColor = [UIColor clearColor];
    
    //注册
    [mGridView registerClass:[GridCell class] forCellWithReuseIdentifier:@"cell"];
    //设置代理
    mGridView.delegate = self;
    mGridView.dataSource = self;
    [self.view addSubview:mGridView];
    
    
    
    
}

- (void) addGallery
{
    UIImageView *adGallery = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 120)];
    [adGallery setImage:[UIImage imageNamed:@"more_gallery_holder"]];
    [self.view addSubview:adGallery];
    
    //NSString *url = [[NSString alloc] initWithFormat:@"%@", API_MORE_GALLERY];
    NSString *url = [[NSString alloc] initWithFormat:@"%@", API_MORE_GALLERY];
    
   [Gallery getDataWithBlock:^(NSArray *data, NSError *error) {
        
        if (!error) {
            
            MoreGalleryView *galleryView = [[MoreGalleryView alloc] initWithFrame:CGRectMake(0, 0, 320, 120) :data];
            
            [adGallery removeFromSuperview];
            [self.view addSubview:galleryView];
        } else {
            NSLog(@"%@", error);
        }
         
    } :url];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    [MobClick beginLogPageView:@"LiveViewController"];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [MobClick endLogPageView:@"LiveViewController"];
}


//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    GridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    /*
    if (cell == nil) {
        cell = [[GridCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:CellIdentifier];
    }*/
    
    cell.grid = [mDataList objectAtIndex:(NSUInteger)indexPath.row];
    return cell;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake([[UIScreen mainScreen]bounds].size.width/3.0, 80);
}

//定义每个UICollectionView 的 margin

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //cell.backgroundColor = [UIColor blueColor];
    
    if (indexPath.row == 0) {
        FavoriteViewController *favCtr = [[FavoriteViewController alloc] initWithNibName:nil bundle:nil];
        favCtr.hidesBottomBarWhenPushed = YES; // 详情页隐藏tabbar
        
        
        //self.navigationController.navigationBar.barTintColor = MAIN_COLOR;
        
        [self.navigationController pushViewController:favCtr animated:YES];
    }
    /*
    // 夜间模式
    else if (indexPath.row == 1) {
        
        
        BOOL isNight = [Util isNightMode];
        [[NSUserDefaults standardUserDefaults] setBool:!isNight forKey:@"isNightMode"];
        [mGridView reloadData];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"themeChange" object:self userInfo:nil];
        
        
        UITabBarController *tabbar = self.tabBarController;
        if (!isNight) {
            tabbar.tabBar.barTintColor = ColorWithRGB(4, 4, 4);
        } else {
            tabbar.tabBar.barTintColor = ColorWithRGB(236, 236, 236);
        }
     
        RecommendViewController *recCtr = [[RecommendViewController alloc] init];
        recCtr.hidesBottomBarWhenPushed = YES; // 详情页隐藏tabbar
        
        [self.navigationController pushViewController:recCtr animated:YES];
        
        
    }*/
     else if (indexPath.row == 1) {
        
        ActViewController *actCtr = [[ActViewController alloc] init];
        actCtr.hidesBottomBarWhenPushed = YES; // 详情页隐藏tabbar
        
        [self.navigationController pushViewController:actCtr animated:YES];
    
    } else if (indexPath.row == 2) {
        
        FeedbackViewController *feedCtr = [[FeedbackViewController alloc] init];
        feedCtr.hidesBottomBarWhenPushed = YES; // 详情页隐藏tabbar
        
        [self.navigationController pushViewController:feedCtr animated:YES];
        
        
    } else if (indexPath.row == 3) {
        
        DeclarationViewController *decCtr = [[DeclarationViewController alloc] init];
        decCtr.hidesBottomBarWhenPushed = YES; // 详情页隐藏tabbar
        
        [self.navigationController pushViewController:decCtr animated:YES];
        
        
    } else if (indexPath.row == 4) {
        [Util showMess:@"检测中..." inView:self.view];
        [Util dissmissProcessInView:self.view];
        [Util dismissSuccess:@"暂无新版本" inView:self.view];
        
    } else if (indexPath.row == 5) {
        [Util dismissSuccess:@"清理成功" inView:self.view];
        
    } else {
        [Util dismissSuccess:@"敬请期待" inView:self.view];
    }
    
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

@end
